package work.timejay.shop.service.shop.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.util.CollectionUtils;
import work.timejay.common.ApiPageEntity;
import work.timejay.dto.shop.ShopOperationDTO;
import work.timejay.dto.shop.ShopSearchDTO;
import work.timejay.enums.ErrorMsg;
import work.timejay.exception.TimeJayException;
import work.timejay.shop.entity.shop.Shop;
import work.timejay.shop.entity.shop.ShopExtends;
import work.timejay.shop.mapper.shop.ShopMapper;
import work.timejay.shop.service.shop.ShopExtendsService;
import work.timejay.shop.service.shop.ShopService;
import work.timejay.common.SuperServiceImpl;
import org.springframework.stereotype.Service;
import work.timejay.utils.FileUtils;
import work.timejay.vo.shop.ShopInfoItemVO;
import work.timejay.vo.shop.ShopInfoVO;
import work.timejay.vo.shop.ShopItemVO;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 项目表 服务实现类
 * </p>
 *
 * @author 王文杰
 * @since 2024-02-05
 */
@Service
public class ShopServiceImpl extends SuperServiceImpl<ShopMapper, Shop> implements ShopService {

    @Autowired
    private FileUtils fileUtils;

    @Autowired
    private ShopExtendsService shopExtendsService;


    @Override
    public ApiPageEntity<ShopItemVO> shopPages(ShopSearchDTO<Shop> dto) {
        QueryWrapper<Shop> qw = new QueryWrapper<>();
        qw.lambda()
                .eq(StringUtils.isNotBlank(dto.getType()), Shop::getTypeId, dto.getType());
        if ("1".equals(dto.getStatus())) {
            qw.lambda()
                    .orderByDesc(Shop::getCreateTime);
        }
        if ("2".equals(dto.getStatus())) {
            qw.lambda()
                    .orderByDesc(Shop::getGiveNum);
        }
        if (!"0".equals(dto.getModel())) {
            qw.lambda()
                    .eq(StringUtils.isNotBlank(dto.getModel()), Shop::getModel, dto.getModel());
        }
        Page<Shop> page = baseMapper.selectPage(dto.getPage(), qw);
        List<ShopItemVO> voList = new ArrayList<>();
        page.getRecords().forEach(e -> {
            ShopItemVO bean = BeanUtil.toBean(e, ShopItemVO.class);
            bean.setShopPath(fileUtils.preview(bean.getShopPath()));
            voList.add(bean);
        });
        return ApiPageEntity.create(page, voList);
    }

    @Override
    public void operation(ShopOperationDTO dto) {
        Shop byId = getById(dto.getId());
        if (null == byId) {
            throw TimeJayException.fail(ErrorMsg.SHOP_IS_NULL);
        }
        if ("0".equals(dto.getOperationType())) {
            byId.setGiveNum(byId.getGiveNum() + 1);
        }
        if ("1".equals(dto.getOperationType())) {
            byId.setLikeNum(byId.getLikeNum() + 1);
        }
        if ("2".equals(dto.getOperationType())) {
            byId.setShareNum(byId.getShareNum() + 1);
        }
        updateById(byId);
    }

    @Override
    public ShopInfoVO findInfoById(String id) {
        Shop shop = getById(id);
        if (null == shop) {
            throw TimeJayException.fail(ErrorMsg.SHOP_IS_NULL);
        }
        List<ShopExtends> shopExtends = shopExtendsService.findInfosByShopId(shop.getId());
        if (CollectionUtils.isEmpty(shopExtends)) {
            ShopInfoVO shopInfoVO = BeanUtil.toBean(shop, ShopInfoVO.class);
            shopInfoVO.setShopId(shop.getId());
            shopInfoVO.setShopName(shop.getName());
            shopInfoVO.setShopPath(fileUtils.preview(shop.getShopPath()));
            shopInfoVO.setTotalNum(0);
            shopInfoVO.setInfoItems(new ArrayList<>());
            return shopInfoVO;
        }
        Map<Integer, List<ShopExtends>> shopExtendsMap = shopExtends.stream()
                .collect(Collectors.groupingBy(ShopExtends::getType));
        List<ShopInfoItemVO> voList = new ArrayList<>();
        long totalNum = 0;
        for (Map.Entry<Integer, List<ShopExtends>> entry : shopExtendsMap.entrySet()) {
            List<ShopExtends> value = entry.getValue();
            String name = value.get(0).getName();
            totalNum += value.size();
            List<String> paths = value.stream()
                    .map(ShopExtends::getPath)
                    .collect(Collectors.toList());
            paths.forEach(e -> {
                e = fileUtils.preview(e);
            });
            ShopInfoItemVO vo = new ShopInfoItemVO(name, paths);
            voList.add(vo);
        }
        ShopInfoVO shopInfoVO = BeanUtil.toBean(shop, ShopInfoVO.class);
        shopInfoVO.setShopId(shop.getId());
        shopInfoVO.setShopName(shop.getName());
        shopInfoVO.setShopPath(fileUtils.preview(shop.getShopPath()));
        shopInfoVO.setTotalNum(totalNum);
        shopInfoVO.setInfoItems(voList);
        return shopInfoVO;
    }
}
